package com.wxb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxb.api.common.ErrorCode;
import com.wxb.api.exception.BusinessException;
import com.wxb.api.mapper.UserInterfaceMapper;
import com.wxb.api.service.UserInterfaceService;

import com.wxb.common.model.entity.UserInterface;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
* @author 12796
* @description 针对表【user_interface(接口用户信息)】的数据库操作Service实现
* @createDate 2023-08-03 16:34:07
*/
@Service
@Slf4j
public class UserInterfaceServiceImpl extends ServiceImpl<UserInterfaceMapper, UserInterface>
    implements UserInterfaceService {

    @Resource
    private RedissonClient redissonClient;
    @Override
    public void validUserInterface(UserInterface userInterface, boolean add) {
        if (userInterface == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (add){
            if (userInterface.getInterfaceId() < 0 || userInterface.getUserId() <0){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口或用户不存在");
            }
        }
        if (userInterface.getLeftNum() < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"剩余次数不能小于0");

        }

    }

    @Override
    public boolean invokeCount(long interfaceId, long userId) {
        if (interfaceId <= 0 || userId <=0 ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UpdateWrapper<UserInterface> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interfaceId",interfaceId);
        updateWrapper.eq("userId",userId);
        RLock lock = redissonClient.getLock("wxb:interface:invoke:lock");
        try {
            if (lock.tryLock(0,-1, TimeUnit.MILLISECONDS)){
                System.out.println("getLock"+Thread.currentThread().getId());
                // updateWrapper.gt("leftNum",0);
                updateWrapper.setSql("leftNum = leftNum - 1,totalNum = totalNum +1 ");
                boolean result = this.update(updateWrapper);
            };
        } catch (InterruptedException e) {
            log.error("invoke error:",e);
        }finally {
            if (lock.isHeldByCurrentThread()){
                System.out.println("unlock:"+Thread.currentThread().getId());
                lock.unlock();
            }
        }
//        UpdateWrapper<UserInterface> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("interfaceId",interfaceId);
//        updateWrapper.eq("userId",userId);
////        updateWrapper.gt("leftNum",0);
//        updateWrapper.setSql("leftNum = leftNum - 1,totalNum = totalNum +1 ");
//        boolean result = this.update(updateWrapper);
        return this.update(updateWrapper);
    }

}




